//
//    Tokaplot
//    https://code.google.com/p/tokaplot/
//
//    License: GPL v3
//    https://code.google.com/p/tokaplot/wiki/License
//
//    This file is part of Tokaplot.
//
//    Tokaplot is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    Tokaplot is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Tokaplot.  If not, see <http://www.gnu.org/licenses/>
//

package com.tokaplot;

import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;

//import android.util.Log;

public class WidgetInfo {
	
	private final static int PREFS_VERSION = 2;
	
	private final static String EXTRA_APPWIDGET_SIZE 		= "com.tokaplotplus.Tokaplot.APPWIDGET_SIZE";
	private final static String EXTRA_APPWIDGET_SAMP 		= "com.tokaplotplus.Tokaplot.APPWIDGET_SAMP";
	private final static String EXTRA_APPWIDGET_TYPE1 		= "com.tokaplotplus.Tokaplot.APPWIDGET_TYPE1";
	private final static String EXTRA_APPWIDGET_TYPE2 		= "com.tokaplotplus.Tokaplot.APPWIDGET_TYPE2";
	private final static String EXTRA_APPWIDGET_COL1 		= "com.tokaplotplus.Tokaplot.APPWIDGET_COL1";
	private final static String EXTRA_APPWIDGET_COL2 		= "com.tokaplotplus.Tokaplot.APPWIDGET_COL2";
	private final static String EXTRA_APPWIDGET_STYLE1 		= "com.tokaplotplus.Tokaplot.APPWIDGET_STYLE1";
	private final static String EXTRA_APPWIDGET_STYLE2 		= "com.tokaplotplus.Tokaplot.APPWIDGET_STYLE2";
	private final static String EXTRA_APPWIDGET_BACK_COL1 	= "com.tokaplotplus.Tokaplot.APPWIDGET_BACK_COL1";
	private final static String EXTRA_APPWIDGET_BACK_COL2 	= "com.tokaplotplus.Tokaplot.APPWIDGET_BACK_COL2";
	private final static String EXTRA_APPWIDGET_GRID_STYLE	= "com.tokaplotplus.Tokaplot.APPWIDGET_GRID_STYLE";
	private final static String EXTRA_APPWIDGET_GRID_COL	= "com.tokaplotplus.Tokaplot.APPWIDGET_GRID_COL";
	private final static String EXTRA_APPWIDGET_TEXT_COL	= "com.tokaplotplus.Tokaplot.APPWIDGET_TEXT_COL";
	private final static String EXTRA_APPWIDGET_PFLAGS 		= "com.tokaplotplus.Tokaplot.APPWIDGET_PFLAGS";
	private final static String EXTRA_APPWIDGET_LAST_ID 	= "com.tokaplotplus.Tokaplot.APPWIDGET_LAST_ID";	
	
	private final static int INFO_PLOT_FLAG_SHOW_TEXT1 	= 0x01;
	private final static int INFO_PLOT_FLAG_SHOW_TEXT2 	= 0x02;
	private final static int INFO_PLOT_FLAG_SHOW_MAXMIN = 0x04;
	private final static int INFO_PLOT_FLAG_LRANGE 		= 0x08;
	private final static int INFO_PLOT_FLAG_PAUSED 		= 0x10;
	private final static int INFO_PLOT_FLAG_NEEDSUPDATE	= 0x20;
	
	private final static long samp_Rate_Default 		= 60000;
	private final static String plot1_Type_Default 		= TokaplotService.s_battery_level;
	private final static String plot2_Type_Default 		= TokaplotService.s_cpu_usage;
	private final static int plot1_Col_Default 			= 0xDFFF5252;
	private final static int plot2_Col_Default 			= 0xDF527DFF;
	private final static int plot1_Style_Default 		= 0x1010;
	private final static int plot2_Style_Default 		= 0x1010;
	private final static int plot_Back_Col1_Default 	= 0x9B000000;
	private final static int plot_Back_Col2_Default 	= 0x9B333333;
	private final static int plot_Grid_Style_Default	= 0x1004;
	private final static int plot_Grid_Col_Default		= 0xFF5F5F5F;
	private final static int plot_Text_Col_Default		= 0x7FFFFFFF;
	private final static int plot_Flags_Default 		= 
		INFO_PLOT_FLAG_SHOW_TEXT1 | INFO_PLOT_FLAG_SHOW_TEXT2 | INFO_PLOT_FLAG_NEEDSUPDATE;	

	private int widgetId;			// Widget ID
	private int sizeType;			// Widget size type
	private long samp_Rate;			// Sample rate in microseconds
	private String plot1_Type;		// Plot 1 type
	private String plot2_Type;		// Plot 2 type
	private int plot1_Col;			// Plot 1 colour
	private int plot2_Col;			// Plot 2 colour
	private int plot1_Style;		// Plot 1 line style
	private int plot2_Style;		// Plot 2 line style
	private int plot_Flags;			// Render flags
	private int plot_Back_Col1;		// Background colour 1
	private int plot_Back_Col2;		// Background colour 2
	private int plot_Grid_Style;	// Grid type
	private int plot_Grid_Col;		// Grid type
	private int plot_Text_Col;		// Grid text colour
	private int widgetId_last;		// Last created widget ID
	
	int getID() { return widgetId; }
	long getSampRate() 	{ return samp_Rate; }
	String getType1() 	{ return plot1_Type; }
	String getType2() 	{ return plot2_Type; }
	int getCol1() 		{ return plot1_Col; }
	int getCol2() 		{ return plot2_Col; }
	int getStyle1() 	{ return plot1_Style; }
	int getStyle2() 	{ return plot2_Style; }
	int getFlags() 		{ return plot_Flags; }
	int getBackCol1() 	{ return plot_Back_Col1; }
	int getBackCol2() 	{ return plot_Back_Col2; }
	int getGridType() 	{ return (plot_Grid_Style&0x0F); }
	int getGridStyle() 	{ return (plot_Grid_Style>>0x04)&0xFFF; }
	int getGridCol() 	{ return plot_Grid_Col; }
	int getTextCol() 	{ return plot_Text_Col; }
	int getIDLast()		{ return widgetId_last; }
	
	void setSampRate(long p_samp_Rate) 	{ samp_Rate = p_samp_Rate; }
	void setType1(String p_plot1_Type) 	{ plot1_Type = p_plot1_Type; }
	void setType2(String p_plot2_Type) 	{ plot2_Type = p_plot2_Type; }
	void setCol1(int p_plot1_Col) 		{ plot1_Col = p_plot1_Col; }
	void setCol2(int p_plot2_Col) 		{ plot2_Col = p_plot2_Col; }
	void setStyle1(int p_plot1_Style) 	{ plot1_Style = p_plot1_Style; }
	void setStyle2(int p_plot2_Style) 	{ plot2_Style = p_plot2_Style; }
	void setBackCol1(int p_back_col1) 	{ plot_Back_Col1 = p_back_col1; }
	void setBackCol2(int p_back_col2) 	{ plot_Back_Col2 = p_back_col2; }
	void setGridType(int p_grid_Style_Type) {
		plot_Grid_Style &=0xFFF0;
		plot_Grid_Style |= (p_grid_Style_Type&0x0F);
	}
	void setGridStyle(int p_grid_Style) {
		plot_Grid_Style &=0x0F;
		plot_Grid_Style |= ((p_grid_Style&0xFFF)<<0x04);
	}
	void setGridCol(int p_grid_col) { plot_Grid_Col = p_grid_col; }
	void setTextCol(int p_text_col) { plot_Text_Col = p_text_col; }
	void setIDLast(int p_last) { widgetId_last = p_last; }
	
	boolean getShowText1() 		{ return (plot_Flags&INFO_PLOT_FLAG_SHOW_TEXT1) != 0; }
	boolean getShowText2() 		{ return (plot_Flags&INFO_PLOT_FLAG_SHOW_TEXT2) != 0; }
	boolean getShowMaxMin() 	{ return (plot_Flags&INFO_PLOT_FLAG_SHOW_MAXMIN) != 0; }
	boolean getUseLocalRange() 	{ return (plot_Flags&INFO_PLOT_FLAG_LRANGE) != 0; }
	boolean getPause() 			{ return (plot_Flags&INFO_PLOT_FLAG_PAUSED) != 0; }
	boolean getNeedsUpdate() 	{ return (plot_Flags&INFO_PLOT_FLAG_NEEDSUPDATE) != 0; }
	
	void setShowText1() 		{ plot_Flags|=INFO_PLOT_FLAG_SHOW_TEXT1; }
	void unsetShowText1() 		{ plot_Flags&=(~INFO_PLOT_FLAG_SHOW_TEXT1); }
	void setShowText2() 		{ plot_Flags|=INFO_PLOT_FLAG_SHOW_TEXT2; }
	void unsetShowText2() 		{ plot_Flags&=(~INFO_PLOT_FLAG_SHOW_TEXT2); }
	void setShowMaxMin() 		{ plot_Flags|=INFO_PLOT_FLAG_SHOW_MAXMIN; }
	void unsetShowMaxMin() 		{ plot_Flags&=(~INFO_PLOT_FLAG_SHOW_MAXMIN); }
	void setUseLocalRange() 	{ plot_Flags|=INFO_PLOT_FLAG_LRANGE; }
	void unsetUseLocalRange() 	{ plot_Flags&=(~INFO_PLOT_FLAG_LRANGE); }
	void setPause() 			{ plot_Flags|=INFO_PLOT_FLAG_PAUSED; }
	void unsetPause() 			{ plot_Flags&=(~INFO_PLOT_FLAG_PAUSED); }
	void setNeedsUpdate() 		{ plot_Flags|=INFO_PLOT_FLAG_NEEDSUPDATE; }
	void unsetNeedsUpdate() 	{ plot_Flags&=(~INFO_PLOT_FLAG_NEEDSUPDATE); }
	
	boolean is21() { if (sizeType == 21) return true; return false; }
	boolean is22() { if (sizeType == 22) return true; return false; }
	boolean is41() { if (sizeType == 41) return true; return false; }
	boolean is42() { if (sizeType == 42) return true; return false; }
	
	WidgetInfo(int pId, int pSizeType) {
		
		widgetId 		= pId;
		sizeType 		= pSizeType;
		samp_Rate 		= samp_Rate_Default;
		plot1_Type 		= plot1_Type_Default;
		plot2_Type 		= plot2_Type_Default;
		plot1_Col 		= plot1_Col_Default;
		plot2_Col 		= plot2_Col_Default;
		plot1_Style 	= plot1_Style_Default;
		plot2_Style		= plot2_Style_Default;
		plot_Flags 		= plot_Flags_Default;
		plot_Back_Col1 	= plot_Back_Col1_Default;
		plot_Back_Col2 	= plot_Back_Col2_Default;
		plot_Grid_Style = plot_Grid_Style_Default;
		plot_Grid_Col	= plot_Grid_Col_Default;
		plot_Text_Col	= plot_Text_Col_Default;
		widgetId_last	= -1;
	}
	WidgetInfo(WidgetInfo pWidgetInfo) {
		widgetId 		= pWidgetInfo.widgetId;
		sizeType 		= pWidgetInfo.sizeType;
		samp_Rate 		= pWidgetInfo.samp_Rate;
		plot1_Type 		= pWidgetInfo.plot1_Type;
		plot2_Type 		= pWidgetInfo.plot2_Type;
		plot1_Col 		= pWidgetInfo.plot1_Col;
		plot2_Col 		= pWidgetInfo.plot2_Col;
		plot1_Style 	= pWidgetInfo.plot1_Style;
		plot2_Style 	= pWidgetInfo.plot2_Style;
		plot_Flags 		= pWidgetInfo.plot_Flags;
		plot_Back_Col1 	= pWidgetInfo.plot_Back_Col1;
		plot_Back_Col2 	= pWidgetInfo.plot_Back_Col2;
		plot_Grid_Style	= pWidgetInfo.plot_Grid_Style;
		plot_Grid_Col	= pWidgetInfo.plot_Grid_Col;
		plot_Text_Col	= pWidgetInfo.plot_Text_Col;
		widgetId_last	= pWidgetInfo.widgetId_last;
	}
	WidgetInfo(Intent intent) {
    	Bundle extras = intent.getExtras();
    	widgetId 		= extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    	plot1_Type 		= extras.getString(EXTRA_APPWIDGET_TYPE1);
    	plot2_Type 		= extras.getString(EXTRA_APPWIDGET_TYPE2);
    	sizeType 		= extras.getInt(EXTRA_APPWIDGET_SIZE, 		21); // Use 2x1 size as default
    	samp_Rate 		= extras.getLong(EXTRA_APPWIDGET_SAMP, 		samp_Rate_Default);
    	plot1_Col 		= extras.getInt(EXTRA_APPWIDGET_COL1, 		plot1_Col_Default);
    	plot2_Col 		= extras.getInt(EXTRA_APPWIDGET_COL2, 		plot2_Col_Default);
    	plot1_Style		= extras.getInt(EXTRA_APPWIDGET_STYLE1,		plot1_Style_Default);
    	plot2_Style		= extras.getInt(EXTRA_APPWIDGET_STYLE2,		plot2_Style_Default);
    	plot_Flags 		= extras.getInt(EXTRA_APPWIDGET_PFLAGS,		plot_Flags_Default);
    	plot_Back_Col1 	= extras.getInt(EXTRA_APPWIDGET_BACK_COL1, 	plot_Back_Col1_Default);
    	plot_Back_Col2 	= extras.getInt(EXTRA_APPWIDGET_BACK_COL2, 	plot_Back_Col2_Default);
    	plot_Grid_Style	= extras.getInt(EXTRA_APPWIDGET_GRID_STYLE, plot_Grid_Style_Default);
    	plot_Grid_Col	= extras.getInt(EXTRA_APPWIDGET_GRID_COL, 	plot_Grid_Col_Default);
    	plot_Text_Col	= extras.getInt(EXTRA_APPWIDGET_TEXT_COL, 	plot_Text_Col_Default);
    	widgetId_last	= extras.getInt(EXTRA_APPWIDGET_LAST_ID, 	-1);
	}
	
	void getInfoFromIntent(Intent intent) {
    	Bundle extras = intent.getExtras();
    	widgetId 		= extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    	plot1_Type 		= extras.getString(EXTRA_APPWIDGET_TYPE1);
    	plot2_Type 		= extras.getString(EXTRA_APPWIDGET_TYPE2);
    	sizeType 		= extras.getInt(EXTRA_APPWIDGET_SIZE, 		21); // Use 2x1 size as default
    	samp_Rate 		= extras.getLong(EXTRA_APPWIDGET_SAMP, 		samp_Rate_Default);
    	plot1_Col 		= extras.getInt(EXTRA_APPWIDGET_COL1, 		plot1_Col_Default);
    	plot2_Col 		= extras.getInt(EXTRA_APPWIDGET_COL2, 		plot2_Col_Default);
    	plot1_Style		= extras.getInt(EXTRA_APPWIDGET_STYLE1,		plot1_Style_Default);
    	plot2_Style		= extras.getInt(EXTRA_APPWIDGET_STYLE2,		plot2_Style_Default);
    	plot_Flags 		= extras.getInt(EXTRA_APPWIDGET_PFLAGS, 	plot_Flags_Default);
    	plot_Back_Col1 	= extras.getInt(EXTRA_APPWIDGET_BACK_COL1, 	plot_Back_Col1_Default);
    	plot_Back_Col2 	= extras.getInt(EXTRA_APPWIDGET_BACK_COL2, 	plot_Back_Col2_Default);
    	plot_Grid_Style	= extras.getInt(EXTRA_APPWIDGET_GRID_STYLE,	plot_Grid_Style_Default);
    	plot_Grid_Col	= extras.getInt(EXTRA_APPWIDGET_GRID_COL, 	plot_Grid_Col_Default);
    	plot_Text_Col	= extras.getInt(EXTRA_APPWIDGET_TEXT_COL, 	plot_Text_Col_Default);
    	widgetId_last	= extras.getInt(EXTRA_APPWIDGET_LAST_ID, 	-1);
	}
	void setIntentWithInfo(Intent intent) {
		intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
		intent.putExtra(EXTRA_APPWIDGET_TYPE1, 		plot1_Type);
		intent.putExtra(EXTRA_APPWIDGET_TYPE2, 		plot2_Type);
		intent.putExtra(EXTRA_APPWIDGET_SIZE, 		sizeType);
		intent.putExtra(EXTRA_APPWIDGET_SAMP, 		samp_Rate);
		intent.putExtra(EXTRA_APPWIDGET_COL1, 		plot1_Col);
		intent.putExtra(EXTRA_APPWIDGET_COL2, 		plot2_Col);
		intent.putExtra(EXTRA_APPWIDGET_STYLE1, 	plot1_Style);
		intent.putExtra(EXTRA_APPWIDGET_STYLE2, 	plot2_Style);
		intent.putExtra(EXTRA_APPWIDGET_PFLAGS, 	plot_Flags);
		intent.putExtra(EXTRA_APPWIDGET_BACK_COL1, 	plot_Back_Col1);
		intent.putExtra(EXTRA_APPWIDGET_BACK_COL2, 	plot_Back_Col2);
		intent.putExtra(EXTRA_APPWIDGET_GRID_STYLE, plot_Grid_Style);
		intent.putExtra(EXTRA_APPWIDGET_GRID_COL, 	plot_Grid_Col);
		intent.putExtra(EXTRA_APPWIDGET_TEXT_COL, 	plot_Text_Col);
		intent.putExtra(EXTRA_APPWIDGET_LAST_ID, 	widgetId_last);
	}
	
	void savePreferences(Context context) {
		
		// Save all preferences
		String wPrefix = "TP_" + widgetId + "_";
        SharedPreferences mPrefs = context.getSharedPreferences("TP", Context.MODE_PRIVATE);        
        SharedPreferences.Editor ed = mPrefs.edit();
        ed.putInt(wPrefix + "prefs_version", 	PREFS_VERSION);
        ed.putString(wPrefix + "plot1_Type",	plot1_Type);
        ed.putString(wPrefix + "plot2_Type",	plot2_Type);
        ed.putLong(wPrefix + "samp_Rate", 		samp_Rate);
        ed.putInt(wPrefix + "plot1_Col", 		plot1_Col);
        ed.putInt(wPrefix + "plot2_Col", 		plot2_Col);
        ed.putInt(wPrefix + "plot1_Style", 		plot1_Style);
        ed.putInt(wPrefix + "plot2_Style", 		plot2_Style);
        ed.putInt(wPrefix + "plot_Flags",		plot_Flags);
        ed.putInt(wPrefix + "plot_Back_Col1", 	plot_Back_Col1);
        ed.putInt(wPrefix + "plot_Back_Col2", 	plot_Back_Col2);
        ed.putInt(wPrefix + "plot_Grid",		plot_Grid_Style);
        ed.putInt(wPrefix + "plot_Grid_Col",	plot_Grid_Col);
        ed.putInt(wPrefix + "plot_Text_Col",	plot_Text_Col);
       	ed.commit();
	}
	
	void loadPreferences(Context context) {
		
		// Load all preferences
		String wPrefix = "TP_" + widgetId + "_";
        SharedPreferences mPrefs = context.getSharedPreferences("TP", Context.MODE_PRIVATE);
        
        samp_Rate 		= mPrefs.getLong(wPrefix + "samp_Rate", 	samp_Rate_Default);
        plot1_Type 		= mPrefs.getString(wPrefix + "plot1_Type", 	plot1_Type_Default);
        plot2_Type 		= mPrefs.getString(wPrefix + "plot2_Type", 	plot2_Type_Default);
        plot1_Col 		= mPrefs.getInt(wPrefix + "plot1_Col", 		plot1_Col_Default);
        plot2_Col 		= mPrefs.getInt(wPrefix + "plot2_Col", 		plot2_Col_Default);
        plot1_Style		= mPrefs.getInt(wPrefix + "plot1_Style", 	plot1_Style_Default);
        plot2_Style		= mPrefs.getInt(wPrefix + "plot2_Style", 	plot2_Style_Default);
        plot_Flags 		= mPrefs.getInt(wPrefix + "plot_Flags",		plot_Flags_Default);
        plot_Back_Col1 	= mPrefs.getInt(wPrefix + "plot_Back_Col1",	plot_Back_Col1);
        plot_Back_Col2 	= mPrefs.getInt(wPrefix + "plot_Back_Col2",	plot_Back_Col2);
        plot_Grid_Style	= mPrefs.getInt(wPrefix + "plot_Grid", 		plot_Grid_Style_Default);
        plot_Grid_Col	= mPrefs.getInt(wPrefix + "plot_Grid_Col", 	plot_Grid_Col_Default);
        plot_Text_Col	= mPrefs.getInt(wPrefix + "plot_Text_Col", 	plot_Text_Col_Default);

	}
	
	void copyPreferences(Context context, int wID) {
		
		// Copy preferences from existing widget
		if (wID<0) return;
		
		String wPrefix = "TP_" + wID + "_";
        SharedPreferences mPrefs = context.getSharedPreferences("TP", Context.MODE_PRIVATE);
        
        samp_Rate 		= mPrefs.getLong(wPrefix + "samp_Rate", 	samp_Rate_Default);
        plot1_Col 		= mPrefs.getInt(wPrefix + "plot1_Col", 		plot1_Col_Default);
        plot2_Col 		= mPrefs.getInt(wPrefix + "plot2_Col", 		plot2_Col_Default);
        plot1_Style		= mPrefs.getInt(wPrefix + "plot1_Style", 	plot1_Style_Default);
        plot2_Style		= mPrefs.getInt(wPrefix + "plot2_Style", 	plot2_Style_Default);
        plot_Flags 		= mPrefs.getInt(wPrefix + "plot_Flags",		plot_Flags_Default);
        plot_Back_Col1 	= mPrefs.getInt(wPrefix + "plot_Back_Col1",	plot_Back_Col1);
        plot_Back_Col2 	= mPrefs.getInt(wPrefix + "plot_Back_Col2",	plot_Back_Col2);
        plot_Grid_Style	= mPrefs.getInt(wPrefix + "plot_Grid", 		plot_Grid_Style_Default);
        plot_Grid_Col	= mPrefs.getInt(wPrefix + "plot_Grid_Col", 	plot_Grid_Col_Default);
        plot_Text_Col	= mPrefs.getInt(wPrefix + "plot_Text_Col", 	plot_Text_Col_Default);

	}
	
	void clearPreferences(Context context) {
		
    	// Clear preference data
		String wPrefix = "TP_" + widgetId + "_";
        SharedPreferences mPrefs = context.getSharedPreferences("TP", Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = mPrefs.edit();
        ed.remove(wPrefix + "samp_Rate");
        ed.remove(wPrefix + "sync_Rate");
        ed.remove(wPrefix + "plot1_Type");
        ed.remove(wPrefix + "plot2_Type");
        ed.remove(wPrefix + "plot1_Col");
        ed.remove(wPrefix + "plot2_Col");
        ed.remove(wPrefix + "plot1_Style");
        ed.remove(wPrefix + "plot2_Style");
        ed.remove(wPrefix + "plot_Flags");
        ed.remove(wPrefix + "plot_Back_Col1");
        ed.remove(wPrefix + "plot_Back_Col2");
        ed.remove(wPrefix + "plot_Grid");
        ed.remove(wPrefix + "plot_Grid_Col");
        ed.remove(wPrefix + "plot_Text_Col");
        ed.commit();
	}
}